package com.ljy.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Id;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljy.common.util.BicOrgUtil;
import com.ljy.common.util.PageQuery;
import com.ljy.common.util.PageUtils;
import com.ljy.dao.GoodsDao;
import com.ljy.dao.HolidayDao;
import com.ljy.dao.UserDao;
//import com.ljy.dao.UserDao;
import com.ljy.dao.UserinfoMapper;
import com.ljy.dto.NewPageInfo;
import com.ljy.dto.PageResultDTO;
import com.ljy.entity.Holiday;
import com.ljy.entity.User;
import com.ljy.entity.Userinfo;


@RestController
@RequestMapping("/action")
public class Test {
	private static Logger logger = LoggerFactory.getLogger(Test.class);
	@Autowired
	private UserinfoMapper userinfoMapper;
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private GoodsDao goodsDao;

	@Autowired
	private HolidayDao holidayDao;
	
	@RequestMapping("/query")
	public void testmapper(){
		//这里是采用mapper.xml,不过要注意的是mapper.xml自动生成的增删改查要删掉，
		System.out.println(userinfoMapper.queryinfoByName("admin").getName());
	
	}
	@RequestMapping("/select")
	public void add(){
		//这里是通用mapper
		
		List<Userinfo> list = userinfoMapper.selectAll();
		System.out.println(list.get(0).getName());
		
	}
	
	//主键自增时返回插入主键
	@RequestMapping("/add")
	public void adduser(){
		Userinfo userinfo = new Userinfo();
		
		userinfo.setName("gdfgdfghfh");
		
		Userinfo userinfo2 = new Userinfo();
		
		userinfo2.setName("xcxx");
		
		//插入并返回主键id(在实体类上进行注解)
		userinfoMapper.insert(userinfo);
		System.out.println(userinfo.getUid()+"========================");
		
	}
	
	
	//主键为uuid时自动返回主键
	
	@RequestMapping("/addHolidayByUuid")
	public void addHoliday(){
		Holiday holiday = new Holiday();
		
		holiday.setYearMonth("2018-08");
		holiday.setCreateTime(new Date());
		holiday.setUpdateTime(new Date());
		
		holidayDao.insert(holiday);
		
		System.out.println(holiday.getId());
		
	}
	
	
	
	
	/**
	 * 演示批量插入并返回主键(实体必须有叫id作为主键的属性,就算数据库表中主键不叫id，可以用@Column(name = "name")
	 *)起别名
	 *如：
	 *	@GeneratedValue(strategy = GenerationType.AUTO)
	 	@Column(name = "day_id")
	 	@Id
		private String id;
	 *
	 *
	 */
	@RequestMapping("/add2")
	public void adduser2(){
		User userinfo = new User();
		
		userinfo.setUsername("ahshhshah");
		userinfo.setAge(15);
		
		User userinfo2 = new User();
		
		userinfo2.setUsername("xcxx");
		userinfo2.setAge(15);
		
		
		//批量插入并返回主键
		List<User> recordList = new ArrayList<User>();
		recordList.add(userinfo);
		recordList.add(userinfo2);
		userDao.insertList(recordList);
		List<Long> idList = BicOrgUtil.fetchProperties(recordList, "id");
		System.out.println(idList.toString());
		
	}
	
	//这种方式一般不用
	@RequestMapping("/test1")
	public void testPage(){
		PageHelper.startPage(1, 1);
		Page<Userinfo> page = userinfoMapper.queryAll();
		System.out.println(page.getTotal());
		// 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
		PageInfo<Userinfo> pageInfo = new PageInfo<Userinfo>(page);
		System.out.println(pageInfo.getList().get(0).getName()+pageInfo.getTotal());
	}
	
	
	@RequestMapping("/test2")
	public PageInfo<Userinfo> testPage2(@RequestParam("pageNo")Integer pageNo, @RequestParam("PageSize")Integer PageSize){
		//分页是在查询之前就设置好，查询的时候会自动分页
		PageHelper.startPage(pageNo, PageSize);
		List<Userinfo> list = userinfoMapper.queryAll();
		
		System.out.println(list.size());
		// 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
		PageInfo<Userinfo> pageInfo = new PageInfo<Userinfo>(list);
		System.out.println(pageInfo.getTotal());
		
		//如果把查询到的结果集进行一系列操作后重新进行塞进PageInfo中，只会把结果集和总数计算出来
		//不会重新进行分页，这时需要手动设置返回的pageNo，PageSize
		//重新把新集合进行分页
		List<Userinfo> newList = list.subList(0,2);
		PageInfo<Userinfo> newPageInfo = new PageInfo<Userinfo>(newList);
		newPageInfo.setPageNum(pageNo);
		newPageInfo.setPageSize(PageSize);
		System.out.println(newPageInfo.getList().get(0).getName()+newPageInfo.getTotal());
		return newPageInfo;
	}
	
	/**
	 * 自定义返回参数（这个是直接修改PageInfo源码）
	 * @param pageNo
	 * @param PageSize
	 * @return
	 */
	
	@RequestMapping("/test3")
	public NewPageInfo<Userinfo> testPage3(@RequestParam("pageNo")Integer pageNo, @RequestParam("PageSize")Integer PageSize){
		PageHelper.startPage(pageNo, PageSize);
		List<Userinfo> list = userinfoMapper.queryAll();
		
		System.out.println(list.size());
		// 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
		NewPageInfo<Userinfo> newPageInfo = new NewPageInfo<Userinfo>(list);
			
		System.out.println(newPageInfo.getList().get(0).getName()+newPageInfo.getTotal());
		return newPageInfo;
	}
	
	/**
	 * 自定义返回参数2
	 * @param pageNo
	 * @param PageSize
	 * @return
	 */
	@RequestMapping("/test4")
	public PageResultDTO<Userinfo> testPage4(@RequestParam("pageNo")Integer pageNo, @RequestParam("PageSize")Integer PageSize){
		PageHelper.startPage(pageNo, PageSize);
		List<Userinfo> list = userinfoMapper.queryAll();
		
		System.out.println(list.size());
		// 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
		PageInfo<Userinfo> pageInfo = new PageInfo<Userinfo>(list);
		
		PageResultDTO<Userinfo> userDtoPageResultDTO = new PageResultDTO<Userinfo>(pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getList());

			
		System.out.println(pageInfo.getList().get(0).getName()+pageInfo.getTotal());
		return userDtoPageResultDTO;
	}
	
	/**
	 * 需要多表联查分页时采用这个
	 * @return
	 */
	@RequestMapping("/test5")
	public PageUtils getGoods(){
		
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put("page", 1);
		params.put("limit", 2);
		
		//查询列表数据
		PageQuery query = new PageQuery(params);
		
		List<Map<String,Object>>  goodslist =  goodsDao.getPageList(query);
		Integer total = Integer.valueOf(goodslist.get(0).get("count").toString()) ;
		
		PageUtils pageUtil = new PageUtils(goodslist, total, query.getLimit(), query.getPage());

		return pageUtil;
		
	}
	
}
